# -*- coding:utf-8 -*-

"""
=============================
this is a feibot product,
all rights reserved.
www.feibot.com
@author: Gu Tianyuan
@email: g@feibot.com
@time: ${DATE}:${TIME}
@product: ${PRODUCT_NAME}
=============================
"""
import os.path
import gtyIO.gtyIoTools

import sys

import gtyTools.tools

if __name__ == "__main__":
    # 模块的导入，参考: https://blog.csdn.net/gvfdbdf/article/details/52084144
    for d in ['gtyConfig', 'gtyIO', 'gtySerial', 'gtySocket', 'gtyTools', 'gtyUI']:
        module_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), d)
        sys.path.append(module_path)  # 导入的绝对路径

from gtyTools import gtyTypes, tools


# 记录文件处理类
class RecordFileHandle:

    def __init__(self):
        # 配置的控制接口
        self.configHandlers = gtyTypes.ConfigHandlers()

        try:
            self.machineId = tools.getMachineId(self.configHandlers.mmc.read("machine", "machineId"))
        except Exception as e:
            print(e)
            self.machineId = "U001"

        self.eventId = ''
        self.new_event_id = ""
        with open('./Newevent.ini', 'r') as f:
            self.new_event_id = f.readline().strip()
            f.close()
        # 最大的行数
        self.maxLineNum = self.configHandlers.machine.read("IO", "recordFileMaxLineNum", "int", 5000)
        if self.maxLineNum < 1000:
            self.maxLineNum = 1000

        # oss前缀
        self.ossObjectPrefix = self.configHandlers.machine.read("oss", "ossObjectPrefix", "string", "")
        self.ossLogFilePrefix = self.configHandlers.machine.read("oss", "ossLogFilePrefix", "string", "")

        # 关于存储文件，每一个赛事用一个文件夹，文件名以 设备id_赛事id_record_编号.csv表示
        # 总路径
        self.recordFileRoot = self.configHandlers.machine.read("IO", "dataFileDir")
        # 赛事文件夹
        self.recordFileDirPath = ""
        # 文件序号
        self.fileId = 1
        # 当前文件名
        self.recordFileNameNow = ""
        self.lineNumNow = 0
        # 完整路径
        self.recordFileUrl = ""
        # 生成路径
        self.updateRecordFileUrl()
        # 已经上传过的文件路径列表
        self.fileUrlUploadedList = []

        # 提供下载的段数据文件名
        self.sectionFileName = self.configHandlers.machine.read("IO", "sectionFileName")

        # 客户
        self.formatnum = self.configHandlers.machine.read("formatepc", "formatnum", "string", None)
        self.recordFileNameNowTXT = ""
        self.recordFileUrlTXT = ""
        if self.formatnum in ["format4", "format5"]:
            self.updateRecordFileUrlTXT()

    # 更新一次存储文件位置
    def updateRecordFileUrl(self, newFile=False):
        print(__file__, 'updateRecordFileUrl', newFile)
        # 1. 从配置文件中读取赛事名称
        if newFile:
            self.configHandlers = gtyTypes.ConfigHandlers()
        self.eventId = self.configHandlers.event.read("event", "eventId")

        # 2. 得到此赛事的记录文件夹
        if self.new_event_id == "":
            self.recordFileDirPath = os.path.join(self.recordFileRoot, self.machineId + "_" + self.eventId)
        else:
            self.recordFileDirPath = os.path.join(self.recordFileRoot, self.machineId +"_"+self.new_event_id)
        try:
            os.system("mkdir " + self.recordFileDirPath)
            os.system('rm ' + os.path.join(self.recordFileDirPath, 'zi*'))
        except Exception as e:
            print(e)
        # 3. 列出所有文件名
        files = os.listdir(self.recordFileDirPath)
        resultFiles = []
        for i in files:
            i_str = str(i)
            if (
                    ".zip" not in i_str and
                    ".csv" in i_str and
                    "_" in i_str and
                    not "sync" in i_str and
                    not i_str.endswith(".tmp")
            ):
                resultFiles.append(str(i))
        # 对于目录下多于一个文件的情况
        if len(resultFiles) > 0:
            maxFile = str(max(resultFiles))
            maxFileUrl = os.path.join(self.recordFileDirPath, maxFile)
            try:
                if not newFile:
                    lineNum = getFileLineNum(maxFileUrl)
                    if lineNum < self.maxLineNum:
                        # 沿用原来的文件
                        self.lineNumNow = lineNum
                        self.recordFileNameNow = str(maxFile)
                        self.recordFileUrl = os.path.join(self.recordFileDirPath, self.recordFileNameNow)
                        return
            except Exception as e:
                print(__file__, e)
            # 创建下一个文件
            self.lineNumNow = 0
            self.recordFileNameNow = self.fileAddOne(self.machineId, self.eventId, maxFile)
            self.recordFileUrl = os.path.join(self.recordFileDirPath, self.recordFileNameNow)
            os.system("touch " + self.recordFileUrl)
            print(__file__, "create next file")
        else:
            # 创建第一个文件
            self.lineNumNow = 0
            if self.new_event_id == "":
                self.recordFileNameNow = self.machineId + "_" + self.eventId + "_" + "00001.csv"
            else:
                self.recordFileNameNow = self.machineId + "_" +self.new_event_id + "_" + "00001.csv"
            print(__file__, "create the first file")
            self.recordFileUrl = os.path.join(self.recordFileDirPath, self.recordFileNameNow)
            print(__file__,"recordFileUrl:",self.recordFileUrl)
            os.system("touch " + self.recordFileUrl)

    # 秘鲁客户需要保存到txt
    def updateRecordFileUrlTXT(self, newFile=False):
        print(__file__, 'updateRecordFileUrlTXT', newFile)
        # 3. 列出所有文件名
        files = os.listdir(self.recordFileDirPath)
        resultFilesTXT = []
        for i in files:
            i_str = str(i)
            if (
                    ".zip" not in i_str and
                    ".txt" in i_str and
                    "_" in i_str and
                    not "sync" in i_str and
                    not i_str.endswith(".tmp")
            ):
                resultFilesTXT.append(str(i))
        # 对于目录下多于一个文件的情况
        if len(resultFilesTXT) > 0:
            maxFileTXT = str(max(resultFilesTXT))
            maxFileUrlTXT = os.path.join(self.recordFileDirPath, maxFileTXT)
            try:
                if not newFile:
                    lineNum = getFileLineNum(maxFileUrlTXT)
                    if lineNum < self.maxLineNum:
                        # 沿用原来的文件
                        self.lineNumNow = lineNum
                        self.recordFileNameNowTXT = str(maxFileTXT)
                        self.recordFileUrlTXT = os.path.join(self.recordFileDirPath, self.recordFileNameNowTXT)
                        return
            except Exception as e:
                print(__file__, e)
            # 创建下一个文件
            self.lineNumNow = 0
            self.recordFileNameNowTXT = fileAddOneTXT(self.machineId, self.eventId, maxFileTXT)
            self.recordFileUrlTXT = os.path.join(self.recordFileDirPath, self.recordFileNameNowTXT)
            os.system("touch " + self.recordFileUrlTXT)
            print(__file__, "create next txt file")
        else:
            # 创建第一个文件
            self.lineNumNow = 0
            self.recordFileNameNowTXT = self.machineId + "_" + self.eventId + "_" + "00001.txt"
            print(__file__, "create the first txt file")
            self.recordFileUrlTXT = os.path.join(self.recordFileDirPath, self.recordFileNameNowTXT)
            print(__file__,"recordFileUrlTXT:",self.recordFileUrlTXT)
            os.system("touch " + self.recordFileUrlTXT)

    # 获得当前的文件路径
    def getRecordFileUrl(self):
        #print(__file__,"self.recordFileUrl=",self.recordFileUrl)
        return self.recordFileUrl

    def getRecordFileUrlTXT(self):
        #print(__file__,"self.recordFileUrlTXT=",self.recordFileUrlTXT)
        return self.recordFileUrlTXT

    # 获得已经完成存储的文件列表
    def getFileUrlListRecordDone(self, allFiles=False):
        files = os.listdir(self.recordFileDirPath)
        files.sort()
        res = []
        for f in files:
            if "zip" not in f:
                url = os.path.join(self.recordFileDirPath, f)
                if allFiles:
                    if url != self.recordFileUrl:
                        res.append(url)
                else:
                    if url not in self.fileUrlUploadedList and url != self.recordFileUrl:
                        res.append(url)
        return res

    # 记录已经完整上传的文件路径
    def fileUrlUploadedDone(self, url):
        if url != self.recordFileUrl:
            self.fileUrlUploadedList = self.fileUrlUploadedList + [url]

    # 删除结果文件
    def deleteDataFile(self):
        try:
            print(__file__, "delete eventData")
            os.system("rm -rf " + self.recordFileDirPath)
            self.updateRecordFileUrl()
            return True
        except Exception as e:
            print(__file__, e, "delete data file failed", self.recordFileDirPath)
            self.updateRecordFileUrl()
            return False

    # 截取结果文件，返回数据列表
    # 返回值为：成功与否，字符串，数据列表
    def getDataSection(self, startEnd):
        start = None
        end = None
        # 如果指定了边界，则划定边界
        if len(startEnd) == 2:
            startTimeString = startEnd[0]
            endTimeString = startEnd[1]

            # 首先检查两个时间戳是否合规，大小关系正确
            check1, start = gtyTools.tools.buildDateTime(startTimeString, -1)
            check2, end = gtyTools.tools.buildDateTime(endTimeString, 1)
            print(__file__, check1, start, check2, end)
            if not check1:
                return False, 'failed: start date time limit format wrong', []
            if not check2:
                return False, 'failed: end date time limit format wrong', []
            if start >= end:
                return False, 'failed: the start date and time are greater than the end date and time', []
            print(__file__, 'time stamp format ok')

        # 找到所有csv文件的列表
        files = os.listdir(self.recordFileDirPath)
        files.sort()
        res = []
        for f in files:
            if 'zip' not in f and '.csv' in f and '_' in f:
                url = os.path.join(self.recordFileDirPath, f)
                res.append(url)
        if len(res) < 1:
            return False, 'failed: no data record file found', []

        # 遍历所有的csv文件，把其中符合时间区间的数据放入列表
        dataInSection = []
        for resFile in res:
            f = open(resFile)
            line = 'aa'
            while line:
                line = f.readline()
                try:
                    # 对于没有指定上下限的情况
                    if start is None and end is None:
                        dataInSection.append(line.strip())
                    # 对于指定了上下限的情况
                    else:
                        if line.count(',') >= 2:
                            if self.configHandlers.machine.read("formatepc", "formatnum") in["format2", "format8"] :
                                timeString = line.split(",")[1]
                            else:
                                timeStringParts = line.split(",")[0].split(":")
                                timeString = timeStringParts[1] + ":" + timeStringParts[2] + ":" + timeStringParts[3]
                            check, t = gtyTools.tools.buildDateTime(timeString)
                            if check and start <= t < end:
                                dataInSection.append(line.strip())
                except Exception as e:
                    print(e)
        if len(dataInSection) < 1:
            return False, 'failed: there is no data within the specified date and time range', []
        print(__file__,'dataInSection:',len(dataInSection))
        return True, 'successful', dataInSection

    # 截取结果文件，并写入指定位置
    def packDataSection(self, startEnd):
        res, info, dataInSection = self.getDataSection(startEnd)
        print(__file__,res,info,len(dataInSection))
        if not res:
            return info
        # 将列表中的数据写入目标位置
        webFileUrl = os.path.join('/var/www/html', self.sectionFileName)
        try:
            if gtyIO.gtyIoTools.recordLines(webFileUrl, dataInSection):
                return 'successful'
            else:
                return 'failed: update data file wrong'
        except Exception as e:
            print(__file__,e)
            return 'failed: write to file failed'


# 在现有文件名的基础上增加一个
    def fileAddOne(self,machineId, eventId, fileName):
        try:
            parts = str.split(fileName, '.')
            parts2 = str.split(parts[-2], '_')
            oldId = int(parts2[-1])
            if self.new_event_id == "":
                newFileName = machineId + "_" + eventId + "_" + "%05d" % (oldId + 1) + ".csv"
            else:
                newFileName = machineId + "_"+self.new_event_id + "_" + "%05d" % (oldId + 1) + ".csv"
            return newFileName
        except Exception as e:
            print(e)

def fileAddOneTXT(machineId, eventId, fileName):
    try:
        parts = str.split(fileName, '.')
        parts2 = str.split(parts[-2], '_')
        oldId = int(parts2[-1])
        newFileName = machineId + "_" + eventId + "_" + "%05d" % (oldId + 1) + ".txt"
        return newFileName
    except Exception as e:
        print(e)

# 获得文件的行数
def getFileLineNum(url):
    count = 0
    theFile = open(url, 'r')
    while True:
        buffer = theFile.read(8192 * 1024)
        if not buffer:
            break
        count += buffer.count('\n')
    theFile.close()
    return count


if __name__ == "__main__":
    from gtyTools import gtyLog
    processes = []
    app = RecordFileHandle()
    app.fileAddOne('U001_990_00002.csv')
